#include <math.h>
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <QGraphicsScene>
#include <QPainter>
#include <QStyleOption>
#include "carvis.h"

 static const double Pi = 3.14;
 static double TwoPi = 2.0 * Pi;

 static qreal normalizeAngle(qreal angle)
 {
     while (angle < 0)
         angle += TwoPi;
     while (angle > TwoPi)
         angle -= TwoPi;
     return angle;
 }
 // --> Constructor Parametry: kierunek(k¹t), szybkosc, kolor
 Carvis::Carvis(int CarId) : angle(0), speed(0),color(qrand() % 256, qrand() % 256, qrand() % 256)
 {
	 this->CarId = CarId;
	 setRotation(270);
 }

 QRectF Carvis::boundingRect() const
 {
     qreal adjust = 0.5;
     return QRectF(-18 - adjust, -22 - adjust, 38 + adjust, 62 + adjust);
 }

 QPainterPath Carvis::shape() const
 {
     QPainterPath path;
     path.addRect(-10, -20, 20, 40);
     return path;
 }
 // --> Funkcja rysuj¹ca 
 void Carvis::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
 {
     // --> Szkielet
     painter->setBrush(color);
	 painter->drawRoundedRect(0, 0, 20, 40, 24, 3);	
	 // --> wiat³a
	 painter->setBrush(Qt::GlobalColor::yellow);
     painter->drawRect(0, 1, 5, 3);
	 painter->drawRect(15, 1, 5, 3);
 }

 void Carvis::changed(const QList<QRectF> & region)
 {
	 qreal dx = ::sin(angle) * 10;
	 QRectF qrectf = region.at(0); 
	 qrectf.setHeight(dx);
 }

 void Carvis::advance(int step)
 {
     if (!step)
         return;
	 
	 speed = 60;
     qreal dx = ::sin(angle) * 90;
		
	
 }
 void Carvis::zrob_ruch(QPoint currentpos, int Car_Id)
 {
	
	int x, y, i, j;
	QPoint nowa = currentpos;
	QPoint stara;
		if (Car_Id == CarId)
		{
			x = nowa.x()*20;
			y = nowa.y()*20;
			setPos(x,y);
		}
		
 }


 